package com.knife.blog.web.action;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

//import com.knife.blog.domain.BlogCircle;
import com.knife.blog.domain.BlogInfo;
import com.knife.blog.domain.BlogNewsDoc;
import com.knife.blog.logic.BlogCircleDocService;
import com.knife.blog.logic.BlogCircleService;
import com.knife.blog.logic.impl.BlogCircleDocServiceImpl;
import com.knife.blog.logic.impl.BlogCircleServiceImpl;
import com.knife.util.CommUtil;
import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;
import com.knife.web.tools.IActiveUser;
import com.knife.web.tools.IPageList;

public class BlogCircleDocAction extends BaseCmdAction {
	
	private static BlogCircleDocService circleDocService = BlogCircleDocServiceImpl.getInstance();
	private static BlogCircleService circleService = BlogCircleServiceImpl.getInstance();
	
	public Object doBefore(WebForm form, Module module) {
		BlogInfo circle;
		if (form.get("user") != null) {
			try {
				String username = URLDecoder.decode(URLEncoder.encode(
						(String) form.get("user"), "ISO8859_1"), "utf-8");
				circle = circleService.getBlogCircle(username);
			} catch (Exception e) {
				System.out.println("错误的连接，blog不存在，或者已经被删除!" + e);
				e.printStackTrace();
			}
		}
		return super.doBefore(form, module);
	}

	@Override
	public Page doInit(WebForm form, Module module) {
		// TODO 自动生成方法存根
		BlogInfo circle = (BlogInfo)form2Obj(form);
		Date d2 = new Date();
		Calendar ca = Calendar.getInstance();
		ca.setTime(d2);
		ca.roll(Calendar.DATE, -1);
		Date d1 = ca.getTime();
		List todayHeadList = circleDocService.getTopDoc(circle,
				"displaytime desc", 2, 1).getResult();
		List eliteList = circleDocService.getEliteDoc(circle,"displaytime desc",1,
				1, 5).getResult();
		List newsList = circleDocService.getTopDoc(circle,"displaytime desc", 0,
				15).getResult();
		form.addResult("todayHeadList", todayHeadList);
		form.addResult("eliteList", eliteList);
		form.addResult("list", newsList);
		form.addResult("hotReadList", circleDocService.getTopDoc(circle,"",
				 d1, d2, "readTimes desc",0,0, 0, 10).getResult());
		form.addResult("hotReviewList", circleDocService.getTopDoc(circle,
				"", d1, d2, "replyNum desc",0,0, 0, 10).getResult());
		return module.findPage("circleHome");
	}
	
	public Object form2Obj(WebForm form) {
		String cid = CommUtil.null2String(form.get("cid"));
		BlogNewsDoc obj = circleDocService.getCircleDoc(cid);
		if(obj == null){
			obj = new BlogNewsDoc();
		}
		form.toPo(obj);
		return null;
	}

}
